package com.aaa.controller;

import ch.qos.logback.core.net.SyslogOutputStream;
import com.aaa.entity.Emp;
import com.aaa.entity.Massage;
import com.aaa.service.S_EmpService;
import com.aaa.util.EmpUtil;
import com.aaa.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping("/login/")
public class S_LoginController {
    @Autowired
    S_EmpService service;

    /** 登录
     * @return
     */
    @RequestMapping("logi")
    @ResponseBody
    public Boolean logi(Emp emp){
//      String md5= MD5.MD5(emp.getPassword());
//      emp.setPassword(md5);
        System.out.println(emp.getUsername());
        System.out.println(emp.getPassword());

        //范 加
        Map map1=new HashMap();
        MD5 md5=new MD5();
        String password=md5.MD5(emp.getPassword());
        map1.put("username",emp.getUsername());
        map1.put("password",password);

        Map map=new HashMap();
        map.put("username",emp.getUsername());
        map.put("password",emp.getPassword());
        emp.setPassword(password);//加密密码
        EmpUtil.setEmp(emp);
        Massage  massage=service.session(map1);
        EmpUtil.SaveMessage(massage);//存储单位session    lhd

       // System.out.println(EmpUtil.getMessage().getAddress());
        Boolean row=service.login(map1);
        return  row;
    }

    @RequestMapping("login")
    public String login(){
        return "login/login";
    }

    /**
     *  注销登录
     */
    @RequestMapping("logout")
    public String logoout(){
        EmpUtil.removeEmp();//注销session
        return "redirect:login";
    }

    /**  获取登录用户名
     * @return
     */
    @RequestMapping("name")
    @ResponseBody
    public String name(){
        return EmpUtil.getEmp().getUsername();
    }
}
